//
//  ProgressBtn.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/7/19.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ProgressBtn.h"

@interface ProgressBtn ()

@property (nonatomic, strong) CAShapeLayer *progressLayer;

@property (nonatomic, strong) CAShapeLayer * progressBackgroundLayer;

@property (nonatomic) BOOL isSpinning;



@end

@implementation ProgressBtn

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup {
    
    _lineWidth = fmaxf(self.frame.size.width * 0.025, 1.f);
    _progressColor = [UIColor whiteColor];//[UIColor colorWithRed:39/255.0 green:178/255.0 blue:197/255.0 alpha:1.0];
    
    self.progressBackgroundLayer = [CAShapeLayer layer];
    _progressBackgroundLayer.contentsScale = [[UIScreen mainScreen] scale];
    _progressBackgroundLayer.strokeColor = [UIColor whiteColor].CGColor;
    _progressBackgroundLayer.fillColor = self.backgroundColor.CGColor;
    _progressBackgroundLayer.lineCap = kCALineCapRound;
    _progressBackgroundLayer.lineWidth = _lineWidth;
   // [self.layer addSublayer:_progressBackgroundLayer];
    self.space = 4.0f;
    [self.layer addSublayer:self.progressLayer];
    
}

- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = fmaxf(lineWidth, 1.f);
    
    _progressBackgroundLayer.lineWidth = _lineWidth;
    _progressLayer.lineWidth = _lineWidth * 2;
}


- (void)setProgressColor:(UIColor *)tintColor
{
    _progressColor = tintColor;
    _progressBackgroundLayer.strokeColor = tintColor.CGColor;
    _progressLayer.strokeColor = tintColor.CGColor;

}


- (void)setPlayState:(AVPlayerPlayState)playState {
    
}
- (CAShapeLayer *)progressLayer {
    if (!_progressLayer) {
        
        _progressLayer = [CAShapeLayer layer];
      //  _progressLayer.contentsScale = [[UIScreen mainScreen] scale];
        _progressLayer.strokeColor = _progressColor.CGColor;
        _progressLayer.fillColor = nil;
        _progressLayer.lineCap = kCALineCapSquare;
        _progressLayer.lineWidth = _lineWidth * 2.0;
    }
    
    return  _progressLayer;
}

- (void)setProgress:(CGFloat)progress {
    if (progress > 1.0) progress = 1.0;
    
    if (_progress != progress) {
        _progress = progress;
        
        
        [self setNeedsDisplay];
    }
}


- (void) drawBackgroundCircle:(BOOL) partial {
    CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
    CGFloat endAngle = (2 * (float)M_PI) + startAngle;
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = (self.bounds.size.width - _lineWidth)/2;
    
    // Draw background
    UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
    processBackgroundPath.lineWidth = _lineWidth;
    processBackgroundPath.lineCapStyle = kCGLineCapRound;
    
    // Recompute the end angle to make it at 90% of the progress
    if (partial) {
        endAngle = (1.8F * (float)M_PI) + startAngle;
    }
    
    [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    
    _progressBackgroundLayer.path = processBackgroundPath.CGPath;
}

- (void)startSpinProgressBackgroundLayer {
    self.isSpinning = YES;
    [self drawBackgroundCircle:YES];
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = HUGE_VALF;
    [_progressBackgroundLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}


- (void) stopSpinProgressBackgroundLayer {
    [self drawBackgroundCircle:NO];
    [_progressBackgroundLayer removeAllAnimations];
    self.isSpinning = NO;
}

- (void)drawRect:(CGRect)rect
{
    // Make sure the layers cover the whole view
    _progressBackgroundLayer.frame = self.bounds;
    //_progressLayer.frame = CGRectMake(2, 2, self.bounds.size.width - 4, self.bounds.size.height - 4);
   // _iconLayer.frame = self.bounds;
    
    CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    CGFloat radius = (self.bounds.size.width - _lineWidth)/2;
    
    // Draw background
    //[self drawBackgroundCircle:_isSpinning];
    
    // Draw progress
    CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
    // CGFloat endAngle = (2 * (float)M_PI) + startAngle;
    CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
    UIBezierPath *processPath = [UIBezierPath bezierPath];
    processPath.lineCapStyle = kCGLineCapButt;
    processPath.lineWidth = _lineWidth;
    
    radius = (self.bounds.size.width - _lineWidth*3) / 2.0 - self.space;
    [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    
    [_progressLayer setPath:processPath.CGPath];
    
    //    switch (_circularState) {
    //        case FFCircularStateStop:
    //            [self drawStop];
    //            break;
    //
    //        case FFCircularStateStopSpinning:
    //            [self drawStop];
    //            break;
    //
    //        case FFCircularStateStopProgress:
    //            [self drawStop];
    //            break;
    //
    //        case FFCircularStateCompleted:
    //            [self drawTick];
    //            break;
    //
    //        case FFCircularStateIcon:
    //            if (!self.iconView && !self.iconPath){
    //                [self drawArrow];
    //            }
    //            else if (self.iconPath){
    //                _iconLayer.path = self.iconPath.CGPath;
    //                _iconLayer.fillColor = nil;
    //            }
    //            break;
    //            
    //        default:
    //            break;
    //    }
    
}

@end
